package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Student;
import cn.wolfcode.mapper.StudentMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.StudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    /**
     * 添加或修改
     * @param student
     */
    @Override
    @Transactional
    public void saveOrUpdate(Student student,Long[] roleIds) {
        if (student.getId() != null) {
            //修改操作
            //更新学生自身数据
            studentMapper.updateByPrimaryKey(student);
            //更新中间表数据
            studentMapper.deleteStudentRoomBatch(student.getId());
            studentMapper.deleteStudentRoleBatch(student.getId());
            studentMapper.insertStudentRoomBatch(student.getId(), student.getRoom().getId());
            if (!student.isAdmin() && roleIds != null && roleIds.length > 0) {
                //学生角色中间表
                studentMapper.insertStudentRoleBatch(student.getId(), roleIds);
                //更新宿舍中间表
            }
            //更新宿舍人数

        }else {
            //添加操作
            String salt = UUID.randomUUID().toString().substring(0, 4);
            student.setSalt(salt);
            Md5Hash hash = new Md5Hash(student.getPassword(), salt, 3);
            student.setPassword(hash.toString());
            studentMapper.insert(student);
            //更新中间表
            studentMapper.insertStudentRoomBatch(student.getId(), student.getRoom().getId());
            if (!student.isAdmin() && roleIds != null && roleIds.length > 0) {
                studentMapper.insertStudentRoleBatch(student.getId(), roleIds);
            }
            //更新宿舍人数

        }
    }

    /**
     * 根据学生Id删除
     * @param id
     */
    @Override
    @Transactional
    public void deleteById(Long id) {
        //删除学生自身数据
        studentMapper.deleteByPrimaryKey(id);
        //删除中间表数据
        studentMapper.deleteStudentRoomBatch(id);

    }
    /**
     *通过Id查询学生信息
     * @param id
     * @return
     */
    @Override
    public Student selStudentById(Long id) {
        return studentMapper.selectByPrimaryKey(id);
    }

    /**
     * 展示所有学生信息
     * @return
     */
    @Override
    public List<Student> selAll() {
        return studentMapper.selectAll();
    }

    //分页
    public PageInfo<Student> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Student> students = studentMapper.selForList(qo);
        return new PageInfo<>(students);
    }

    @Override
    public Boolean checkUsername(String username) {
        int count = studentMapper.selectCountUsername(username);
        return count <= 0;
    }

    @Override
    public int selectCountStudents(Long roomId ) {
        return studentMapper.selectCountStudents(roomId);
    }

    @Override
    public Boolean checkRoomPerson(Long roomId) {
        int count = studentMapper.checkRoomPerson(roomId);
        return count <3;
    }
}
